package com.zx.jhpt.login;

import com.zx.framework.core.util.StringUtil;
import com.zx.framework.web.controller.BaseController;
import com.zx.jhpt.excel.Engine;
import com.zx.jhpt.excel.vo.Risk;
import com.zx.jhpt.po.Fxfkjz;
import com.zx.jhpt.service.DynamicService;
import com.zx.jhpt.service.EngineService;
import com.zx.jhpt.service.FxfkjzService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 风险数据
 * Created by FLT on 2016/3/24.
 */
@Controller
@RequestMapping("/dynamic")
public class DynamicController extends BaseController {

    @Resource
    private EngineService engineService;
    @Resource
    private DynamicService dynamicService;
    @Resource
    private FxfkjzService fxfkjzService;

    /**
     * 同步到数据集市
     *
     */
    @RequestMapping(value = "/toDataMartDo")
    @ResponseBody
    public Map<String, Object> toDataMart(@RequestParam(required=false) String fxid,String cycle){
        try{
            List<String> fxids = new ArrayList<>();
            if(StringUtil.isEmpty(fxid)){
                List<Fxfkjz> fxfkjzs = fxfkjzService.getAll();
                for(Fxfkjz fxfkjz:fxfkjzs){
                    fxids.add(fxfkjz.getId());
                }
            }else {
                fxids.add(fxid);
            }

            for(String id:fxids){
                Risk r = Engine.getRisk(id);
                List<Map<String,Object>> datas = engineService.loadDatas(r,cycle);
                dynamicService.dataTransfer(r, datas, cycle);
            }

            return clientMap().success().message("同步成功！").getMap();
        } catch (Exception e) {
            return clientMap().failure().message(e.getMessage()).getMap();
        }
    }
}
